



use "$data/WEEKLY.GOOGLE.dta", clear



merge 1:1 iso2c year month day using "$data/COVID.DAILY.dta"
keep if _merge==1 | _merge==3
drop _merge

encode iso2c, gen(iso)
egen tw = group(year week)
gen tq = yq(year, quarter)
gen tm = ym(year, month)



global googlesearchvar = "recession_hits conspiracy_theory_hits stockmarket_crash_hits prepper_hits"





destring $googlesearchvar, replace force
xtset iso tw

sort iso tw

collapse (mean) $googlesearchvar  covid_confirmed covid_death covid_recovered, by(iso2c iso year month quarter tq tm)

xtset iso tm


collapse (mean) $googlesearchvar  covid_confirmed covid_death covid_recovered, by(iso2c iso year quarter tq)


label variable recession_hits "Recession topic Google searches"

merge 1:1 iso2c year quarter using "$data/EIU_QUARTERLY2020.dta"
keep if _merge==1 | _merge==3

drop _merge
xtset iso tq
sort iso tq

capture label drop interact
label define eiu 1 "realGDP" 2 "ind_production" 3 "consumption" 4 "investment" 5 "govtspending" 6 "export" 7 "import"
global eiu = "eiu_realGDP_pct_D eiu_ind_prod_pct_realD eiu_pc_pct_realD eiu_gfi_pct_realD eiu_gc_pct_realD eiu_export_gs_pct_realD eiu_import_gs_pct_realD"

local nlen :  word count   $eiu

matrix hrid = J(`nlen',6,.)

egen meanrecession_hits = mean(recession_hits), by(iso2c)
replace recession_hits = recession_hits/(meanrecession_hits+1)


local var  recession_hits  
estimates clear
global iter=1
foreach depvar in eiu_realGDP_pct_D eiu_ind_prod_pct_realD eiu_pc_pct_realD eiu_gfi_pct_realD eiu_gc_pct_realD eiu_export_gs_pct_realD eiu_import_gs_pct_realD {


eststo: reghdfe `depvar'   l1.`var'     , absorb(iso tq) vce(cl iso)
	estadd loc empt =  " "
 matrix hrid[$iter,1] = $iter
lincom l1.`var'
 matrix hrid[$iter,3] = `r(estimate)'
 matrix hrid[$iter,2] = `r(estimate)' - 1.95 * `r(se)'
 matrix hrid[$iter,4] = `r(estimate)' + 1.95 * `r(se)'
 matrix hrid[$iter,5] = `r(se)' 
lincom _cons
 matrix hrid[$iter,6] = `r(estimate)' 

global iter = $iter+1

}
esttab using "$tables/eiu-`var'-leadingindicator.tex", drop(_cons) replace  nolines nomtitles fragment nowrap  label nodepvars noobs  starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none) style(tex) cells(b(star fmt(%9.3f)) se(par))  stats(empt r2 N_clust1 N , labels( " " "R$^2$" "Countries" "Observations" ) fmt(%9.3g)) nonumbers

svmat hrid

label values hrid1 eiu
keep hrid*
drop if hrid1==. 
rename hrid1 dv
rename hrid2 lowerci
rename hrid3 est
rename hrid4 upperci
rename hrid5 se
rename hrid6 meandv

export delimited using "$tables/tables_raw/coefs-eiu-`var'.csv", replace     





